import Cocoa

//var str = "Hello, playground"

//Swift中高阶函数的用法
//map:对原始集合里的每一个元素,以一个变换后的元素替换之形成一个新的集合
let numbers = [1, 2, 4, 5, 10]
print(numbers.map{ $0 * 10})  //使用尾随闭包的形式
print(numbers.map({ (s1) -> Int in
   return s1 * 10
}))
//filter:过滤符合条件的元素
print(numbers.filter({ (s1) -> Bool in
   return s1 > 4
}))
//使用尾随闭包的形式(当函数的最后一个参数是函数时，在函数调用时可以把闭包表达式写在()外面)
print(numbers.filter{$0 > 4})
//reduce: 作用于当前累积的结果上
print(numbers.reduce(100, { (r1, r2) -> Int in
    return r1 + r2
}))
//使用尾随闭包的形式
print(numbers.reduce(100, {$0 + $1}))

//flatMap:对于元素是集合的集合,可以得到单级的集合
let arrayNumbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9 ]]
print(arrayNumbers.flatMap{ $0.map{ $0 * 10 } } )


//compactMap:过滤空值
let names: [String?] = ["zhangsan", nil, "lisi", "wangwu", nil,  "zhaoliu"]
print(names.count)  //包含了空值的个数
//过滤掉空值
print(names.compactMap{ $0 })   //打印出来的是不含空值的数组
//打印不是空值字符串的 字符个数
print(names.compactMap{ $0?.count })

//命令式编程:
////在words中获取不包含["a", "of", "and", "or", "the", "on"]这些字符串的所有字符串以及个数
//let words = """
//    There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real dream what you want to dream go where you want to go be what you want to be because you have only one life and one chance to do all the things you want to do
//    """
//let NON_WORDS = ["a", "of", "and", "or", "the", "on"]
//func wordFreq(words: String) -> [String: Int] {
//    //定义一个空字典对象
//    var wordDic: [String: Int] = [:]  //使用字面量的方式定义了一个空的字典对象
//    //将传入的字符串使用空格进行分割
//    let wordList = words.split(separator: " ")
//    //遍历字符串
//    for word in wordList {
//        //将所有的字符串都转化成小写
//        let wordLowercase = word.lowercased()
//        //判断所有已经转成小写字母的字符串中是否有NON_WORDS这个变量中的这些字符串
//        if !NON_WORDS.contains(wordLowercase) {
//            //如果这些字符串中不包含NON_WORDS这个变量中的这些字符串
//            //定义一个count来获取不含NON_WORDS这个变量中的这些字符串的字符串个数
//            if let count = wordDic[wordLowercase] {
//                wordDic[wordLowercase] = count + 1
//            } else {
//                wordDic[wordLowercase] = 1
//            }
//        }
//    }
//    return wordDic
//}
//print(wordFreq(words: words))
//
////函数式编程
//func wordFreq2(words: String) -> [String: Int] {
//    //定义一个空的字典对象
//    var wordDic: [String: Int] = [:]
//    //将传入的字符串以空格分割
//    let wordList = words.split(separator: " ")
//    //遍历所有的字符串,并将所有字符串转化成小写
//    wordList.map{ $0.lowercased()}   //将所有字符串转换成小写
//        .filter{ !NON_WORDS.contains($0)}
//        .forEach { (word) in   //使用forEach遍历,后面一个闭包
//            wordDic[word] = (wordDic[word] ?? 0) + 1
//    }
//    return wordDic
//}
//print(wordFreq2(words: words))  //字典是无序的,两次打印字典中的字符串顺序不一致


//遍历字符串
let charWord:String = "hell, world"
let charArray: [Character] = ["a", "e", "i", "o", "u", "d"]
func charIndex(word: String) -> Int {
    var wordIndex: Int = 0
    for i in word {
        if charArray.contains(i) {
            break
        }
        print("-----------")
        wordIndex += 1
        if wordIndex == word.count {
            return -1
        }
    }
    print("wordIndex is \(wordIndex)")
    return wordIndex
}
charIndex(word: charWord)

var names2 = ["zhangsan", "l", "lisi", "wangwu", "w", "z", "zhaoliu", "s", "n"]
//names2.removeAll{ (name) -> Bool in   //移除字符串中字符个数大于1的字符串,使用了闭包的形式
//    return name.count > 1
//}
//print("names2 is \(names2)")

let results = names2.map{ $0.prefix(1).uppercased() + $0.dropFirst() }
print("results is \(results)")
print(names2.map{ $0.dropFirst()})

//拼接数组元素,转化成了字符串(将数组转换成字符串)
let result = results.joined(separator: ",")
print("result is \(result)")


func cleanName(names: [String]) -> String {
    var cleanName = ""
    for name in names {
        if name.count > 1 {
            cleanName += name.capitalized + ","
            print(cleanName)
        }
    }
    cleanName.remove(at: cleanName.index(before: cleanName.endIndex))
    return cleanName
}
print(cleanName(names: names2))


func namesList(names: [String]) -> String {
    return names.filter{ $0.count > 1 }
        .map{ $0.prefix(1).uppercased() + $0.dropFirst()}
    .joined(separator: ",")   //使用join的将数组转成字符串
}
print(namesList(names: names2))

let result2 = names2.filter{ $0.count > 1 }
    .map{ $0.capitalized }   //让首字母字符串大写
.joined(separator: ",")
print("result2 is \(result2)")



